using UnityEngine;
using System.Collections;
using Mono.Data.Sqlite;

public class RandomName : MonoBehaviour {
	public UILabel nameLabel;
	
	string databasePath;
	SqliteConnectionStringBuilder connstr;
	
	void Start(){
		if(!nameLabel){
			Debug.Log("Please reference the label of playername");
		}
		
		databasePath = Application.dataPath + "/Resources/DataBase/MyDatabase.db";
		connstr = new SqliteConnectionStringBuilder();
		connstr.DataSource = databasePath;
	}
	
	void OnClick(){
		string familyName = "";
		string name = "";
		
		DbAccess db = new DbAccess(connstr.ToString());
		int fn = (int)(Random.value * 10);
		string sql = "select name from RandomFamilyName where id = " + fn.ToString();
		
		SqliteDataReader sdr = db.ExecuteQuery(sql);
		while (sdr.Read()){
			familyName = sdr.GetString(0);
		}
		
		int nameId = (int)(Random.value * 10);
		
		sql = "select name from RandomName where id = " + nameId.ToString();
		sdr = db.ExecuteQuery(sql);
		
		while(sdr.Read()){
			name = sdr.GetString(0);
		}
		
		nameLabel.text = familyName + name;
		
		sdr.Close();
		db.CloseSqlConnection();
	}
}
